package com.v1.video.util;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;

import java.lang.reflect.Field;


/**
 * 提供获取屏幕相关信息和显示相关单位转化
 * <p/>
 * Created by hangl on 2014/8/5.
 */
public class DisplayUtils {

    /*设备分辨率获取*/
    public static DisplayMetrics getDisplayMetrics(Context context) {
        if (context == null) throw new IllegalArgumentException("getDisplayMetrics(),param context is null");
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        display.getMetrics(displayMetrics);
        return displayMetrics;
    }

    /**
     * get screen width
     */
    public static int getWindowWidth(Context context) {
        DisplayMetrics metric = getDisplayMetrics(context);
        return metric.widthPixels;
    }

    /**
     * get screen height
     */
    public static int getWindowHeight(Context context) {
        DisplayMetrics metric = getDisplayMetrics(context);
        return metric.heightPixels;
    }

    /**
     * 判断设备是否处于竖屏状态，该方法仅适用于手机，不适用于pad,tv
     * true 代表竖屏，false为横屏
     *
     * @param ctx
     * @return current device oritation state
     */
    public static boolean isDeviceInPortrait(Context ctx) {
        DisplayMetrics metric = getDisplayMetrics(ctx);
        return metric.widthPixels < metric.heightPixels;
    }

    /**
     * 判断设备是否处于横屏状态，该方法仅适用于手机，不适用于pad,tv
     * true 代表横屏，false为竖屏
     *
     * @param ctx
     * @return
     */
    public static boolean isDeviceInLandscape(Context ctx) {
        DisplayMetrics metric = getDisplayMetrics(ctx);
        return metric.widthPixels > metric.heightPixels;
    }

    /**
     * 将dip转换为px
     *
     * @param dip
     * @return px
     * @author hangl
     */
    public static int convertDipToPixel(Context ctx, int dip) {
        float density = ctx.getResources().getDisplayMetrics().densityDpi / 160f;
        return (int) (dip * density + 0.5);
    }

    /**
     * 将px转换为dip
     *
     * @param context
     * @param pxValue
     * @return
     * @author hangl
     */
    public static int convertPixelToDip(Context context, float pxValue) {
        final float density = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / density + 0.5f);
    }

    /**
     * 设置是否显示顶部状态栏
     *
     * @param activity
     * @param isDisplay
     * @author guoxt
     */
    public static void setDisplayStatusBar(Activity activity, boolean isDisplay) {
        Window win = activity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        if (isDisplay) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    /**
     * 获取顶部状态栏高度
     *
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context) {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0, statusBarHeight = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
        	e1.printStackTrace();
        }
        return statusBarHeight;
    }
    

    public static int getStatusBarHeight5(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}


